package LambdaDemo;

/*@FunctionalInterface //函数式接口
interface NoParameterNoReturn {
    //注意：只能有一个抽象方法
    void test();
    default void test2() {

    }
    public static void test3() {

    }
}*/


import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}


//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}
//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

public class Test {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "bit");
        map.put(3, "hello");
        map.put(4, "lambda");

        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer a, String b) {
                System.out.println("key:"+a+",val:"+b);
            }
        });
        map.forEach((a,b)-> System.out.println("key:"+a+",val:"+b));
    }

    public static void main6(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("bit");
//        list.add("hello");
        list.add("lambda");

        /*list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        //lambda简写
        list.forEach(s-> System.out.println(s));*/

        list.forEach(s-> System.out.println(s));
        System.out.println("===========");
        /*list.sort(new Comparator<String>(){
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });*/
        list.sort((s1,s2) -> {return s1.compareTo(s2);});
        list.forEach(s-> System.out.println(s));

    }


    public static void main5(String[] args) {
        int a=10;
        PriorityQueue<Integer> pq=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
//                a=100;
                System.out.println(a);
                return o1.compareTo(o2);
            }
        });

        PriorityQueue<Integer> pq2=new PriorityQueue<>((o1,o2)->{return o2.compareTo(o1);});
    }

    public static void main4(String[] args) {
        NoParameterReturn noParameterReturn=()->10;
        System.out.println(noParameterReturn.test());
    }

    public static void main3(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = (g)-> System.out.println(g);
        oneParameterNoReturn.test(10);
    }

    public static void main2(String[] args) {
        NoParameterNoReturn noParameterNoReturn =()->System.out.println("调用test");
        noParameterNoReturn.test();
    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            //接口不能实例化，加了花括号，那就是匿名内部类
            @Override
            public void test() {
                System.out.println("调用test");
            }
        };
        noParameterNoReturn.test();
    }
}
